package light.head.dao.inv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvStorage;

@Service
public class InvStorageDAO extends BaseDAO<InvStorage, Integer>{
	
	/**
	 * 查询跟机构下的所有子机构的库存信息
	 * @param ocs 机构编号集合
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public RESULT findByMap(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null || ocs.size() == 0) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvStorage s WHERE s.oc IN(");
		List<Object> list=new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String cname = map.get(Parm.CNAME); //商品编号
			String dname = map.get(Parm.DNAME);
			String fast = map.get(Parm.FAST);
			String on = map.get(Parm.ON);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.deleted = ?");
			}
			if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND s.cc IN (SELECT c.code FROM InvCommodityItem c WHERE c.cname LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}
			if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND s.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if(isNotNull(fast)){
				list.add("%" + fast +"%");
				hql.append(" AND s.fast LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pb) {
		if(oc == null) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvStorage s WHERE s.oc = ?");
		List<Object> list=new ArrayList<Object>();
		list.add(oc);
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String cname = map.get(Parm.CNAME); //商品编号
			String dname = map.get(Parm.DNAME);
			String fast = map.get(Parm.FAST);
			String on = map.get(Parm.ON);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.deleted = ?");
			}
			if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND s.cc IN (SELECT c.code FROM InvCommodityItem c WHERE c.cname LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}
			if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND s.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if(isNotNull(fast)){
				list.add("%" + fast +"%");
				hql.append(" AND s.fast LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 根据机构编号查询库存种无需序列号的商品信息
	 * @param oc 机构编号
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public RESULT findByMap_1(List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null || ocs.size() == 0) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvStorage s WHERE s.deleted = 0 AND s.snc = 1 AND s.oc IN (");
		List<Object> list=new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String cname = map.get(Parm.CNAME); //商品编号
			String dname = map.get(Parm.DNAME);
			String fast = map.get(Parm.FAST);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.delete = ?");
			}
			if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND s.cc IN (SELECT c.code FROM InvCommodityItem c WHERE c.cname LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}
			if(isNotNull(fast)){
				list.add("%" + fast +"%");
				hql.append(" AND s.fast LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	/**
	 * 根据机构编号和仓库编号查询库存种无需序列号的商品信息
	 * @param oc 机构编号
	 * @param map
	 * @param pb 
	 * @return 执行返回分页查询方法
	 */
	public RESULT findByMap_2(Integer dc, List<Integer> ocs, Map<String, String> map, PageBean pb) {
		if(ocs == null) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvStorage s WHERE s.deleted = 0 AND s.snc = 1 AND s.dc = ? AND s.oc IN (");
		List<Object> list=new ArrayList<Object>();
		list.add(dc);
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String cname = map.get(Parm.CNAME); //商品编号
			String dname = map.get(Parm.DNAME);
			String fast = map.get(Parm.FAST);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.delete = ?");
			}
			if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND s.cc IN (SELECT c.code FROM InvCommodityItem c WHERE c.cname LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}if(isNotNull(fast)){
				list.add("%" + fast +"%");
				hql.append(" AND s.fast LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return findToPageBean(hql.toString(), pb, list);
	}
	
	/**
	 * 根据商品编号和仓库编号获取当前跟机构下所有子机构的库存情况
	 */
	public List<InvStorage> findByCcDc(Integer cc,Integer dc, List<Integer> ocs){
		if(null == cc || null == dc || ocs == null || ocs.size() == 0 ) return null;
		
		StringBuffer hql = new StringBuffer("FROM InvStorage ins WHERE ins.deleted = 0 AND ins.cc = ? AND ins.dc = ? AND ins.oc IN (");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(cc);
		parm.add(dc);
		for (Integer i : ocs) {
			if (null != i) {
				hql.append("?,");
				parm.add(i);
			}
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		return find(hql.toString(), parm.toArray());
	}
	/**
	 * 根据商品编号和仓库编号获取出库仓机构下的库存情况
	 */
	public List<InvStorage> findByCcDc(Integer cc,Integer dc, Integer oc){
		if(null == cc || null == dc || oc == null) return null;
		
		StringBuffer hql = new StringBuffer("FROM InvStorage ins WHERE ins.deleted = 0 AND ins.cc = ? AND ins.dc = ? AND ins.oc = ?");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(cc);
		parm.add(dc);
		parm.add(oc);
		return find(hql.toString(), parm.toArray());
	}
	
	/**
	 * 查询跟机构下的所有子机构的库存信息（导出）
	 * @param ocs 机构编号集合
	 * @param map
	 * @return 
	 */
	public List<InvStorage> findByMap1(List<Integer> ocs, Map<String, String> map) {
		if(ocs == null || ocs.size() == 0) return null;
		
		StringBuffer hql= new StringBuffer("FROM InvStorage s WHERE s.oc IN(");
		List<Object> list=new ArrayList<Object>();
		for (Integer i : ocs) {
			hql.append("?,");
			list.add(i);
		}
		hql.deleteCharAt(hql.length() - 1).append(")");
		
		if(map != null){
			String deleted = map.get(Parm.DELETED);
			String cname = map.get(Parm.CNAME); //商品编号
			String dname = map.get(Parm.DNAME);
			String fast = map.get(Parm.FAST);
			String on = map.get(Parm.ON);
			if(isNotNull(deleted)){
				list.add(Boolean.parseBoolean(deleted));
				hql.append(" AND s.deleted = ?");
			}
			if(isNotNull(cname)){
				list.add("%" + cname + "%");
				hql.append(" AND s.cc IN (SELECT c.code FROM InvCommodityItem c WHERE c.cname LIKE ?)");
			}
			if(isNotNull(dname)){
				list.add("%" + dname + "%");
				hql.append(" AND s.dc IN (SELECT d.code FROM InvDepot d WHERE d.name LIKE ?)");
			}
			if (isNotNull(on)) {
				list.add("%" + on + "%");
				hql.append(" AND s.oc IN (SELECT o.code FROM Organ o WHERE o.name LIKE ?)");
			}
			if(isNotNull(fast)){
				list.add("%" + fast +"%");
				hql.append(" AND s.fast LIKE ?");
			}
		}
		hql.append(" ORDER BY s.code DESC");
		return find(hql.toString(), list.toArray());
	}
}
